package com.hippo.core.util.custom;

import cn.binarywang.tools.generator.base.GenericGenerator;
import com.google.common.collect.Maps;

import java.util.Map;
import java.util.Random;


/**
 * The type Hkid card number generator.
 */
public class HKIDCardNumberGenerator extends GenericGenerator {
    private static final GenericGenerator instance = new HKIDCardNumberGenerator();

    private HKIDCardNumberGenerator() {
    }

    /**
     * Gets instance.
     *
     * @return the instance
     */
    public static GenericGenerator getInstance() {
        return instance;
    }


    @Override
    public String generate() {
        Random random = new Random();
        Map<String, String> code = HKIDCardNumberGenerator.getCode();
        int index = (int) (Math.random() * 26) + 1;
        String prefix = code.get(String.valueOf(index)) + (random.nextInt(900000) + 100000);
        int sum = index * 8;
        int num = 7;
        for (int i = 1; i < prefix.length(); i++) {
            sum += (Integer.parseInt(prefix.substring(i, i + 1)) * num);
            num -= 1;
        }
        String suffix;
        if (sum % 11 == 0) {
            suffix = "0";
        } else if (sum % 11 == 1) {
            suffix = "A";
        } else {
            suffix = String.valueOf(11 - sum % 11);
        }
        return prefix + "（" + suffix + "）";
    }

    /**
     * @return Map
     */
    private static Map<String, String> getCode() {
        Map<String, String> map = Maps.newHashMap();
        map.put("1", "A");
        map.put("2", "B");
        map.put("3", "C");
        map.put("4", "D");
        map.put("5", "E");
        map.put("6", "F");
        map.put("7", "G");
        map.put("8", "H");
        map.put("9", "I");
        map.put("10", "J");
        map.put("11", "K");
        map.put("12", "L");
        map.put("13", "M");
        map.put("14", "N");
        map.put("15", "O");
        map.put("16", "P");
        map.put("17", "Q");
        map.put("18", "R");
        map.put("19", "S");
        map.put("20", "T");
        map.put("21", "U");
        map.put("22", "V");
        map.put("23", "W");
        map.put("24", "X");
        map.put("25", "Y");
        map.put("26", "Z");
        return map;
    }

    /**
     * The entry point of application.
     *
     * @param args the input arguments
     */
    public static void main(String[] args) {
//        String a = "Z497577（5）";
//        System.out.println(Integer.parseInt(a.substring(1, 2)) * 4);
//        System.out.println(Character.getNumericValue(a.charAt(1)) * 4);
        for (int i = 0; i < 100; i++) {
            System.out.println(HKIDCardNumberGenerator.getInstance().generate());
        }

    }
}
